package com.md.drcs.dicom.dao;

import com.md.common.utils.dao.MyMapper;
import com.md.drcs.dicom.dto.PatientStudyGenResp;
import com.md.drcs.dicom.dto.UserPatientDTO;
import com.md.drcs.dicom.entity.StudyCollectEntity;
import com.md.drcs.dicom.entity.StudyEntity;
import com.md.drcs.dicom.req.StudyCollectGroupReq;
import com.md.drcs.dicom.sql.StudySQLBuiler;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

import java.time.LocalDateTime;
import java.util.List;

public interface StudyCollectDao extends MyMapper<StudyCollectEntity> {

    /**
     * 非住院放射
     * @param begin
     * @param end
     * @return
     */
    @Select("select count(*) from t_Patient a, t_Study b" +
            " where a.nPatId = b.nPatID" +
            " and b.StuDate  between #{begin} and #{end}" +
            " and b.sMod not in ('ES','US','ECG','JY','PX','BL') " +
            " and b.sStuType in ('门诊')" +
            " and b.bCloseCloud = 0")
    int getCtmr(@Param("begin") LocalDateTime begin, @Param("end") LocalDateTime end);

    /**
     * 住院放射
     * @param begin
     * @param end
     * @return
     */
    @Select("select count(*) from t_Patient a, t_Study b " +
            " where a.nPatId = b.nPatID" +
            " and b.StuDate  between #{begin} and #{end}" +
            " and b.sMod not in ('ES','US','ECG','JY','PX','BL') " +
            " and b.sStuType not in ('门诊')" +
            " and b.bCloseCloud = 0")
    int getHospCtmr(@Param("begin") LocalDateTime begin, @Param("end") LocalDateTime end);

    /**
     * 非住院超声
     * @param begin
     * @param end
     * @return
     */
    @Select("select count(*) from t_Patient a, t_Study b" +
            " where a.nPatId = b.nPatID" +
            " and b.StuDate  between #{begin} and #{end}" +
            " and b.sMod in ('US')" +
            " and b.sStuType in ('门诊')")
    int getUs(@Param("begin") LocalDateTime begin, @Param("end") LocalDateTime end);

    /**
     * 住院超声
     * @param begin
     * @param end
     * @return
     */
    @Select("select count(*) from t_Patient a, t_Study b" +
            " where a.nPatId = b.nPatID" +
            " and b.StuDate  between #{begin} and #{end}" +
            " and b.sMod in ('US')" +
            " and b.sStuType not in ('门诊')")
    int getHospUs(@Param("begin") LocalDateTime begin, @Param("end") LocalDateTime end);


    /**
     * 非住院内镜
     * @param begin
     * @param end
     * @return
     */
    @Select("select count(*) from t_Patient a, t_Study b" +
            " where a.nPatId = b.nPatID" +
            " and b.StuDate  between #{begin} and #{end}" +
            " and b.sMod in ('ES')" +
            " and b.sStuType in ('门诊')")
    int getEs(@Param("begin") LocalDateTime begin, @Param("end") LocalDateTime end);

    /**
     * 住院内镜
     * @param begin
     * @param end
     * @return
     */
    @Select("select count(*) from t_Patient a, t_Study b" +
            " where a.nPatId = b.nPatID" +
            " and b.StuDate  between #{begin} and #{end}" +
            " and b.sMod in ('ES')" +
            " and b.sStuType not in ('门诊')")
    int getHospEs(@Param("begin") LocalDateTime begin, @Param("end") LocalDateTime end);

    /**
     * 检验
     * @param begin
     * @param end
     * @return
     */
    @Select("select count(*) from t_Patient a, t_Study b" +
            " where a.nPatId = b.nPatID" +
            " and b.StuDate  between #{begin} and #{end}" +
            " and b.sMod in ('JY')")
    int getJy(@Param("begin") LocalDateTime begin, @Param("end") LocalDateTime end);


    @SelectProvider(type = StudySQLBuiler.class, method = "getStudyCollectGroup")
    List<UserPatientDTO> getStudyCollectGroup(StudyCollectGroupReq req);

    @Select("select * from t_Patient a, t_Study b" +
            " where a.nPatId = b.nPatID" +
            " and b.StuDate  between #{begin} and #{end}")
    List<PatientStudyGenResp> findByStudyDate(@Param("begin") LocalDateTime begin, @Param("end") LocalDateTime end);


    //新增2025.9.13
    /**
     * 非住院病理
     * @param begin
     * @param end
     * @return
     */
    @Select("select count(*) from t_Patient a, t_Study b" +
            " where a.nPatId = b.nPatID" +
            " and b.StuDate  between #{begin} and #{end}" +
            " and b.sMod in ('BL')" + // " and b.sMod in ('ES')" +
            " and b.sStuType in ('门诊')")
    int getBL(@Param("begin") LocalDateTime begin, @Param("end") LocalDateTime end);

    /**
     * 住院病理
     * @param begin
     * @param end
     * @return
     */
    @Select("select count(*) from t_Patient a, t_Study b" +
            " where a.nPatId = b.nPatID" +
            " and b.StuDate  between #{begin} and #{end}" +
            " and b.sMod in ('BL')" + // " and b.sMod in ('ES')" +
            " and b.sStuType not in ('门诊')")
    int getHospBL(@Param("begin") LocalDateTime begin, @Param("end") LocalDateTime end);

    /**
     * 非住院心电
     * @param begin
     * @param end
     * @return
     */
    @Select("select count(*) from t_Patient a, t_Study b" +
            " where a.nPatId = b.nPatID" +
            " and b.StuDate  between #{begin} and #{end}" +
            " and b.sMod in ('ECG')" + // " and b.sMod in ('ES')" +
            " and b.sStuType in ('门诊')")
    int getECG(@Param("begin") LocalDateTime begin, @Param("end") LocalDateTime end);

    /**
     * 住院心电
     * @param begin
     * @param end
     * @return
     */
    @Select("select count(*) from t_Patient a, t_Study b" +
            " where a.nPatId = b.nPatID" +
            " and b.StuDate  between #{begin} and #{end}" +
            " and b.sMod in ('ECG')" + // " and b.sMod in ('ES')" +
            " and b.sStuType not in ('门诊')")
    int getHospECG(@Param("begin") LocalDateTime begin, @Param("end") LocalDateTime end);

    /**
     * 非住院牙片
     * @param begin
     * @param end
     * @return
     */
    @Select("select count(*) from t_Patient a, t_Study b" +
            " where a.nPatId = b.nPatID" +
            " and b.StuDate  between #{begin} and #{end}" +
            " and b.sMod in ('PX')" + // " and b.sMod in ('ES')" +
            " and b.sStuType in ('门诊')")
    int getPX(@Param("begin") LocalDateTime begin, @Param("end") LocalDateTime end);

    /**
     * 住院牙片
     * @param begin
     * @param end
     * @return
     */
    @Select("select count(*) from t_Patient a, t_Study b" +
            " where a.nPatId = b.nPatID" +
            " and b.StuDate  between #{begin} and #{end}" +
            " and b.sMod in ('PX')" + // " and b.sMod in ('ES')" +
            " and b.sStuType not in ('门诊')")
    int getHospPX(@Param("begin") LocalDateTime begin, @Param("end") LocalDateTime end);


}
